﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMU.Certificates;

namespace CMU.Manage
{
    public partial class UCLopHoc : UserControl
    {
        CertificateEntities context = new CertificateEntities();
        public UCLopHoc()
        {
            InitializeComponent();
        }


        private void UCLopHoc_Load(object sender, EventArgs e)
        {
            var loadlh = from lh in context.LopHoc
                         select lh;
            dataGridView1.DataSource = loadlh.ToList();
            dataGridView1.Refresh();

            var giaovien = from gv in context.GiangVien
                           select gv;
            comboBox1.DataSource = giaovien.ToList<GiangVien>();
            comboBox1.DisplayMember = "TenGV";
            comboBox1.ValueMember = "Id";

            var thoigian = from tg in context.ThoiGian
                           select tg;
            comboBox2.DataSource = thoigian.ToList<ThoiGian>();
            comboBox2.DisplayMember = "NamHoc";
            comboBox3.DataSource = thoigian.ToList<ThoiGian>();
            comboBox3.DisplayMember = "HocKy";
            comboBox2.ValueMember = "Id";

            var monhoc = from mh in context.MonHoc
                         select mh;
            comboBox4.DataSource = monhoc.ToList<MonHoc>();
            comboBox4.DisplayMember = "TenMH";
            comboBox4.ValueMember = "Id";

            var khoahoc = from kh in context.KhoaHoc
                          select kh;
            comboBox5.DataSource = khoahoc.ToList<KhoaHoc>();
            comboBox5.DisplayMember = "TenKH";
            comboBox5.ValueMember = "Id";
        }

        private void button2_Click(object sender, EventArgs e)
        {

            LopHoc lh = new LopHoc();
            lh.Id_GV = (int) comboBox1.SelectedValue;
            lh.Id_TG = (int)comboBox2.SelectedValue;
            lh.Id_MH = (int)comboBox4.SelectedValue;
            lh.Id_KH = (int)comboBox5.SelectedValue;
            

            context.LopHoc.Add(lh);
            int count = context.SaveChanges();
            if (count > 0)
            {
                MessageBox.Show("Da them moi");
                UCLopHoc_Load(sender, e);
            }
            else
                MessageBox.Show("Da ton tai");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int lhID;
            if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out lhID))
            {
                using (CertificateEntities newcontext = new CertificateEntities())
                {
                    LopHoc lh = new LopHoc() { Id = lhID };
                    newcontext.LopHoc.Attach(lh);
                    //context.ObjectStateManger.ChangeObjectState(mh, EntityState.Deleted);
                    //newcontext.Entry(mh).State = System.Data.EntityState.Deleted;
                    newcontext.LopHoc.Remove(lh);

                    int count = newcontext.SaveChanges();
                    if (count > 0)
                    {
                        MessageBox.Show("Da xoa");
                        UCLopHoc_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Mon Hoc da duoc lien ket");
                    }
                   
                }
            }
        }

        // EDIT
        private void button3_Click(object sender, EventArgs e)
        {
            int svID;
            if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out svID))
            {
                using (CertificateEntities context = new CertificateEntities())
                {

                    //SinhVien sinhvien = new SinhVien() { Id = svID };
                    //sinhvien = (from sv in context.SinhVien
                    //            where sv.Id == svID
                    //            select sv).FirstOrDefault<SinhVien>();

                    //combobox1.Text = sinhvien.MaSV;
                    //textBox2.Text = sinhvien.MaMH;
                }
            }
        }

    }
}
